package com.coco.bargain.service.impl;

import com.coco.bargain.feign.GoodsFeignService;
import com.coco.bargain.feign.UserFeignService;
import com.coco.bargain.mapper.BargainGoodsMapper;
import com.coco.bargain.mapper.BargainItemMapper;
import com.coco.bargain.mapper.BargainUserHelpMapper;
import com.coco.bargain.mapper.BargainUserMapper;
import com.coco.bargain.pojo.*;
import com.coco.bargain.service.BargainItemService;
import com.coco.bargain.vo.BargainItemTableSearchVo;
import com.coco.bargain.vo.BargainItemTableVo;
import com.coco.bargain.vo.BargainUserVo;
import com.coco.common.result.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HanSama
 * @date 2021年07月22日 19:21
 */
@Service
public class BargainItemServiceImpl implements BargainItemService {

    @Autowired
    private BargainUserMapper bargainUserMapper;

    @Autowired
    private BargainItemMapper bargainItemMapper;

    @Autowired
    private BargainGoodsMapper bargainGoodsMapper;

    @Autowired
    private BargainUserHelpMapper bargainUserHelpMapper;

    @Autowired
    private GoodsFeignService goodsFeignService;

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public ResultData seach(BargainItemTableSearchVo bargainItemTableSearchVo) {
        PageHelper.startPage(bargainItemTableSearchVo.getPage(),bargainItemTableSearchVo.getLimit());

        //获取所有用户的砍价
        List<TbBargainUser> userList = bargainUserMapper.seach(bargainItemTableSearchVo);
        List<BargainItemTableVo> tableVos = userList.stream().map(barUser -> {
            BargainItemTableVo tableVo = new BargainItemTableVo();
            //设置基本信息
            tableVo.setId(barUser.getId());
            tableVo.setSumCount(barUser.getSumCount());
            tableVo.setResidueCount(barUser.getResidueCount());
            tableVo.setStartTime(barUser.getCreateTime());
            tableVo.setEndTime(barUser.getEndTime());
            tableVo.setStatus(barUser.getStatus());

            //获取价格
            TbBargainItem bargainItem = bargainItemMapper.findOneById(barUser.getBargainItemId());
            tableVo.setBargainPrice(bargainItem.getBargainPrice());

            //获取介绍 和 图片
            TbBargainGoods bargainGoods = bargainGoodsMapper.findById(bargainItem.getBargainGoodsId());
            tableVo.setIntro(bargainGoods.getIntro());
            tableVo.setImgUrl(bargainGoods.getImgUrl());

            //调用GoodsFeign获取Goods信息
            TbGoods oneById = goodsFeignService.findOneById(bargainGoods.getSpuId());
            tableVo.setTitle(oneById.getSpuName());

            return tableVo;
        }).collect(Collectors.toList());
        //分页
        PageInfo<BargainItemTableVo> pageInfo = new PageInfo<>(tableVos);
        //返回结果集
        return new ResultData(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public void add(TbBargainItem tbBargainItem) {
        bargainItemMapper.add(tbBargainItem);
    }

    @Override
    public void update(TbBargainItem tbBargainItem) {
        bargainItemMapper.update(tbBargainItem);
    }

    @Override
    public TbBargainItem findById(Long id) {
        return bargainItemMapper.findById(id);
    }

    @Override
    public void deleteById(Long id) {
        bargainUserMapper.deleteById(id);
    }

    @Override
    public List<BargainUserVo> seeDetails(Long id) {
        //用户自己
        TbBargainUser myBargain = bargainUserMapper.findById(id);
        TbUser mySelf = userFeignService.findOne(myBargain.getUserId()).getData();
        //返回值
        ArrayList<BargainUserVo> bargainUserVos = new ArrayList<>();
        bargainUserVos.add(new BargainUserVo(mySelf.getId(), mySelf.getImgUrl(), mySelf.getUserName(), myBargain.getMyBargainPrice(),myBargain.getCreateTime()));
        //获取帮砍用户记录
        List<TbBargainUserHelp> bargainUserHelpList = bargainUserHelpMapper.findHelpUserByBarginUserId(myBargain.getId());
        if (bargainUserHelpList!=null && bargainUserHelpList.size()>0){
            bargainUserHelpList.forEach(tbBargainUserHelp -> {
                //获取帮砍用户信息
                TbUser helpUser = userFeignService.findOne(tbBargainUserHelp.getUserId()).getData();
                //添加返回值
                bargainUserVos.add(new BargainUserVo(helpUser.getId(), helpUser.getImgUrl(), helpUser.getUserName(), tbBargainUserHelp.getPrice(),tbBargainUserHelp.getCreateTime()));
            });
        }

        return bargainUserVos;
    }
}
